{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# w4_冯炳驹_124298228\n",
    "#采用CH聚类得出一个"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 聚类\n",
    "\n",
    "熟悉各中聚类算法的调用\n",
    "并用评价指标选择合适的超参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:06.811000Z",
     "start_time": "2018-01-12T05:51:01.843000Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#导入必要的工具包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.cluster import MiniBatchKMeans\n",
    "\n",
    "from sklearn import metrics\n",
    "import time\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:06.859000Z",
     "start_time": "2018-01-12T05:51:06.813000Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 38209 entries, 0 to 38208\n",
      "Data columns (total 8 columns):\n",
      "Unnamed: 0         38209 non-null int64\n",
      "user_id            38209 non-null int64\n",
      "LocaleId           38209 non-null int64\n",
      "BirthYearInt       38209 non-null int64\n",
      "GenderId           38209 non-null int64\n",
      "JoinedYearMonth    38209 non-null int64\n",
      "TimezoneInt        38209 non-null int64\n",
      "locationId         38209 non-null int64\n",
      "dtypes: int64(8)\n",
      "memory usage: 2.3 MB\n"
     ]
    }
   ],
   "source": [
    "#读取训练数据\n",
    "train = pd.read_csv('w4_test.csv')\n",
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:06.867000Z",
     "start_time": "2018-01-12T05:51:06.861000Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "user_id = train['user_id']\n",
    "X_train = train.drop(train.columns[0:2],axis=1).values\n",
    "\n",
    "# n_trains = 1000\n",
    "# user_id = train.user_id.values[:n_trains]\n",
    "# X_train = train.drop(train.columns[0:2],axis=1).values[:n_trains]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:06.878000Z",
     "start_time": "2018-01-12T05:51:06.871000Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 230, 1993,    1,   34,  480,  103],\n",
       "       [ 230, 1992,    1,   33,  420,  103],\n",
       "       [ 320, 1975,    1,   34, -240,   40],\n",
       "       ..., \n",
       "       [ 230, 1995,    1,   34,  420,  103],\n",
       "       [ 320, 1989,    1,   34,  420,    0],\n",
       "       [ 320, 1980,    1,   35, -480,    0]], dtype=int64)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:06.885000Z",
     "start_time": "2018-01-12T05:51:06.881000Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the shape of train_image: (38209L, 6L)\n"
     ]
    }
   ],
   "source": [
    "# 原始输入的特征维数和样本数目\n",
    "print('the shape of train_image: {}'.format(X_train.shape))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:06.899000Z",
     "start_time": "2018-01-12T05:51:06.887000Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 一个参数点（聚类数据为K）的模型，在校验集上评价聚类算法性能\n",
    "def K_cluster_analysis(K, X_train):\n",
    "    start = time.time()\n",
    "    \n",
    "    print(\"K-means begin with clusters: {}\".format(K));\n",
    "    \n",
    "    #K-means,在训练集上训练\n",
    "    mb_kmeans = MiniBatchKMeans(n_clusters = K)\n",
    "    mb_kmeans.fit(X_train)\n",
    "    \n",
    "    y_pred = mb_kmeans.predict(X_train)\n",
    "  \n",
    "    # K值的评估标准\n",
    "    CH_score = metrics.calinski_harabaz_score(X_train,y_pred)\n",
    "    \n",
    "    end = time.time()\n",
    "    print(\"CH_score: {}, time elaps:{}\".format(CH_score, int(end-start)))\n",
    "    \n",
    "    return CH_score, mb_kmeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:10.613000Z",
     "start_time": "2018-01-12T05:51:06.906000Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K-means begin with clusters: 5\n",
      "CH_score: 100422.142022, time elaps:0\n",
      "K-means begin with clusters: 10\n",
      "CH_score: 120349.547032, time elaps:0\n",
      "K-means begin with clusters: 20\n",
      "CH_score: 113368.726139, time elaps:0\n",
      "K-means begin with clusters: 40\n",
      "CH_score: 140003.681542, time elaps:0\n",
      "K-means begin with clusters: 60\n",
      "CH_score: 49949.4796644, time elaps:0\n",
      "K-means begin with clusters: 80\n",
      "CH_score: 101301.899275, time elaps:0\n",
      "K-means begin with clusters: 100\n",
      "CH_score: 78733.4780982, time elaps:0\n",
      "K-means begin with clusters: 120\n",
      "CH_score: 101558.486846, time elaps:1\n",
      "K-means begin with clusters: 140\n",
      "CH_score: 78944.6407156, time elaps:0\n"
     ]
    }
   ],
   "source": [
    "# 设置超参数（聚类数目K）搜索范围\n",
    "Ks = [5, 10, 20,40,60,80,100, 120,140]\n",
    "CH_scores = []\n",
    "mb_kmeans = MiniBatchKMeans()\n",
    "best_kmean = MiniBatchKMeans()\n",
    "best_ch_score = -1;\n",
    "\n",
    "for K in Ks:\n",
    "    ch,mb_kmeans = K_cluster_analysis(K, X_train)\n",
    "    CH_scores.append(ch)\n",
    "    if (ch > best_ch_score):\n",
    "        best_ch_score = ch\n",
    "        best_kmean = mb_kmeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:51:10.740000Z",
     "start_time": "2018-01-12T05:51:10.616000Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x8a70be0>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAD8CAYAAACLrvgBAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAAIABJREFUeJzt3XmYHFW5x/Hvmw0SIGRly0IChgBh\nERiydKNeQEJYJCigAZSI0XgRQUR2rqKgggJGUC7ImqCyGVCiFwyBAfFqIAwhG2HJmCCErBqIgJCQ\n5L1/nOqbzmSWnpnururq3+d55unp01Vdbyoz/U6d95xT5u6IiIgUokPcAYiISOVQ0hARkYIpaYiI\nSMGUNEREpGBKGiIiUjAlDRERKZiShoiIFExJQ0RECqakISIiBesUdwDF1qdPHx80aFDcYYiIVJTn\nn3/+H+7et6XtUpc0Bg0aRF1dXdxhiIhUFDP7eyHbqXtKREQKpqQhIiIFU9IQEZGCKWmIiEjBlDRE\nRKRgLSYNM7vTzFaZ2YJGXrvAzNzM+kTPzcxuNLN6M5tnZgfnbTvezBZFX+Pz2g8xs/nRPjeamUXt\nvcxsRrT9DDPrWZx/soiItFUhVxqTgTENG81sAHAU8Hpe8zHAkOhrInBztG0v4ApgBDAcuCIvCdwc\nbZvbL3esS4An3H0I8ET0XEREYtRi0nD3p4E1jbw0CbgIyL9f7Fjgbg+eAXqY2a7A0cAMd1/j7m8B\nM4Ax0Wvd3X2mh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diO\n0O3QUNLOdXMS/7NiZpcTuo5/nWtqZLPYYzazbsDlwHcae7mRtqLEnNaksRQYkPe8P7AspliaZWad\nCQnj1+7+UNS8MncpGT2uiiu+RmSBE8zsNUK33xGEK48eURcKJPN8LwWWuvuz0fOphCSS5HP9SWCJ\nu6929w+Bh4AMyT/X0PR5TfTvppmNB44HTvfN8xGSGvOehD8o5ka/j/2B2Wa2CyWMOa1J4zlgSDTK\npAuhiDUt5pi2EtUC7gBecvef5L00DRgffT8eeLjcsTXF3S919/7uPohwXmvd/XTgSeDkaLNExQzg\n7iuAN8xsaNR0JLCQBJ9rQrfUSDPrFv2s5GJO9LmONHVepwFnRKN7RgJrc91YcTOzMcDFwAnu/u+8\nl6YB48xsGzMbTCguz4ojxnzuPt/dd3L3QdHv41Lg4OhnvXTn2d1T+QUcSxgB8Tfg8rjjaSLGwwiX\njPOAOdHXsYQawRPAouixV9yxNhH/fwB/iL7fg/CLVA/8Btgm7vgaifejQF10vn8H9Ez6uQa+B7wM\nLAB+CWyTtHMN3EuouXwYfXBNaOq8ErpNbop+L+cTRoYlJeZ6Qh0g97t4S972l0cxvwIck5SYG7z+\nGtCn1OdZM8JFRKRgae2eEhGRElDSEBGRgilpiIhIwZQ0RESkYEoaIiJSMCUNEREpmJKGiIgUTElD\nREQK9n9ifs/2jHgX5wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xf919358>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 绘制不同PCA维数下模型的性能，找到最佳模型／参数（分数最高）\n",
    "plt.plot(Ks, np.array(CH_scores), 'b-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-01-12T05:53:00.326000Z",
     "start_time": "2018-01-12T05:53:00.222000Z"
    }
   },
   "outputs": [],
   "source": [
    "fd_submission = pd.DataFrame()\n",
    "fd_submission['user_id'] = user_id\n",
    "fd_submission['ch_pred_result'] = best_kmean.predict(X_train)\n",
    "\n",
    "fd_submission.to_csv(\"w4_ch_clustering_result.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
